package com.szbber.medical.board.ui.fragment.dialog

import android.graphics.Color
import android.os.Bundle
import android.widget.LinearLayout
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.szbber.commen.base.BaseVMFragment
import com.szbber.commen.bean.DoctorOrderBean
import com.szbber.medical.board.R
import com.szbber.medical.board.databinding.BoardFragmentOrderInfoBinding
import com.szbber.medical.board.databinding.BoardItemCarelabelBinding
import com.szbber.medical.board.databinding.BoardItemDoctorOrderBinding
import com.szbber.medical.board.databinding.BoardItemPatientlistBinding
import com.szbber.medical.board.viewModel.OrderInfoViewModel
import com.szbber.medical.board.viewModel.PatientDetailsFragmentViewModel
import kotlinx.android.synthetic.main.board_fragment_order_info.*

class OrderInfoFragment:BaseVMFragment<BoardFragmentOrderInfoBinding ,OrderInfoViewModel>() {

    companion object {
        fun getInstance(cureNo: String?): OrderInfoFragment {
            val sf = OrderInfoFragment()
            sf.cureNo = cureNo
            return sf
        }
    }

    private var adapter: BaseQuickAdapter<DoctorOrderBean.Data, BaseViewHolder>?= null
    var cureNo: String? = null
    private var page=1
    private var loadMore = false

    override fun getLayoutResId(): Int  = R.layout.board_fragment_order_info

    override fun initVM(): OrderInfoViewModel =
        ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(
            OrderInfoViewModel::class.java)

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.orderinfoVm = mViewModel

        mViewModel.doctorOrderList(activity,cureNo,page.toString())
        mViewModel.doctorOrderBean.observe(this){
            observeRecyclerView(it)
        }

        mRefreshLayout.setOnRefreshListener {
            page=1
            loadMore = false
            mViewModel.doctorOrderListNoDialog(activity,cureNo,page.toString())
        }
        mRefreshLayout.setOnLoadMoreListener {
            page++
            loadMore = true
            mViewModel.doctorOrderListNoDialog(activity,cureNo,page.toString())
        }
    }

    private fun observeRecyclerView(doctorOrderBean: DoctorOrderBean){
        if(loadMore){
            mRefreshLayout.finishLoadMore()
            adapter?.addData(doctorOrderBean.data)
            adapter?.notifyDataSetChanged()
        }else {
            mRefreshLayout.finishRefresh()
            adapter = object :
                BaseQuickAdapter<DoctorOrderBean.Data, BaseViewHolder>(R.layout.board_item_doctor_order,
                    doctorOrderBean.data.toMutableList()) {
                override fun convert(holder: BaseViewHolder, item: DoctorOrderBean.Data) {
                    val binding =
                        DataBindingUtil.bind<ViewDataBinding>(holder.itemView) as BoardItemDoctorOrderBinding
                    binding.orderItem = item
                    if (holder.position % 2 == 0) binding.llItem.setBackgroundColor(resources.getColor(
                        R.color.color_E6F3FF))
                    else binding.llItem.setBackgroundColor(resources.getColor(R.color.color_CAE1FF))
                    binding.executePendingBindings()
                }
            }
            mRecyclerView.adapter = adapter
            val manager = LinearLayoutManager(activity)
            mRecyclerView.layoutManager = manager
        }
    }

}